@@ -3,6 +3,9 @@ require 'cgi' |
||
| 3 | 3 |
module Agents |
| 4 | 4 |
class PublicTransportAgent < Agent |
| 5 | 5 |
cannot_receive_events! |
| 6 |
+ |
|
| 7 |
+ default_schedule "every_2m" |
|
| 8 |
+ |
|
| 6 | 9 |
description <<-MD |
| 7 | 10 |
Specify the following user settings: |
| 8 | 11 |
|
@@ -27,13 +30,8 @@ module Agents |
||
| 27 | 30 |
Finally, set the arrival window that you're interested in. E.g., 5 minutes. Events will be created by the agent anytime a new train or bus comes into that time window. |
| 28 | 31 |
|
| 29 | 32 |
alert_window_in_minutes: 5 |
| 30 |
- |
|
| 31 |
- This memory should get cleaned up when timestamp is older than an hour (or something) so that it doesn't fill up all of the Agent's memory. |
|
| 32 | 33 |
MD |
| 33 | 34 |
|
| 34 |
- |
|
| 35 |
- default_schedule "every_2m" |
|
| 36 |
- |
|
| 37 | 35 |
event_description <<-MD |
| 38 | 36 |
Events look like this: |
| 39 | 37 |
{ "routeTitle":"N-Judah",
|
@@ -55,6 +53,7 @@ module Agents |
||
| 55 | 53 |
def stops |
| 56 | 54 |
options["stops"].collect{|a| a.split("|").last}
|
| 57 | 55 |
end |
| 56 |
+ |
|
| 58 | 57 |
def check |
| 59 | 58 |
hydra = Typhoeus::Hydra.new |
| 60 | 59 |
request = Typhoeus::Request.new(check_url, :followlocation => true) |
@@ -79,18 +78,22 @@ module Agents |
||
| 79 | 78 |
hydra.queue request |
| 80 | 79 |
hydra.run |
| 81 | 80 |
end |
| 81 |
+ |
|
| 82 | 82 |
def update_memory(vals) |
| 83 | 83 |
add_to_memory(vals) |
| 84 | 84 |
cleanup_old_memory |
| 85 | 85 |
end |
| 86 |
+ |
|
| 86 | 87 |
def cleanup_old_memory |
| 87 | 88 |
self.memory["existing_routes"] ||= [] |
| 88 | 89 |
self.memory["existing_routes"].reject!{|h| h["currentTime"].to_time <= (Time.now - 2.hours)}
|
| 89 | 90 |
end |
| 91 |
+ |
|
| 90 | 92 |
def add_to_memory(vals) |
| 91 | 93 |
self.memory["existing_routes"] ||= [] |
| 92 | 94 |
self.memory["existing_routes"] << {"stopTag" => vals["stopTag"], "tripTag" => vals["prediction"]["tripTag"], "epochTime" => vals["prediction"]["epochTime"], "currentTime" => Time.now}
|
| 93 | 95 |
end |
| 96 |
+ |
|
| 94 | 97 |
def not_already_in_memory?(vals) |
| 95 | 98 |
m = self.memory["existing_routes"] || [] |
| 96 | 99 |
m.select{|h| h['stopTag'] == vals["stopTag"] &&
|
@@ -98,6 +101,7 @@ module Agents |
||
| 98 | 101 |
h['epochTime'] == vals["prediction"]["epochTime"] |
| 99 | 102 |
}.count == 0 |
| 100 | 103 |
end |
| 104 |
+ |
|
| 101 | 105 |
def default_options |
| 102 | 106 |
{
|
| 103 | 107 |
agency: "sf-muni", |
@@ -111,6 +115,7 @@ module Agents |
||
| 111 | 115 |
errors.add(:base, 'alert_window_in_minutes is required') unless options['alert_window_in_minutes'].present? |
| 112 | 116 |
errors.add(:base, 'stops are required') unless options['stops'].present? |
| 113 | 117 |
end |
| 118 |
+ |
|
| 114 | 119 |
def working? |
| 115 | 120 |
event_created_within?(2) && !recent_error_logs? |
| 116 | 121 |
end |